package com.wxxymakers.cklabrecruit.service;

import com.wxxymakers.cklabrecruit.entity.SysInfo;
import com.wxxymakers.cklabrecruit.entity.SysInfoExample;
import com.wxxymakers.cklabrecruit.mapper.SysInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/11/17 16:50
 * @DESCRIBE 系统信息业务
 */
@Service
public class SysInfoService implements IService<SysInfo> {

    private final SysInfoMapper mapper;

    @Autowired
    public SysInfoService(SysInfoMapper mapper) {
        this.mapper = mapper;
    }

    public boolean switchReg(boolean can) {
        SysInfo info = new SysInfo();
        info.setCanReg(can);
        return mapper.updateByExampleSelective(info, new SysInfoExample()) > 0;
    }

    public boolean canReg() {
        SysInfo first = findFirst();
        if (first != null) {
            return first.getCanReg();
        }
        return false;
    }

    @Override
    @Transactional
    public SysInfo add(SysInfo entity) {
        /*if (mapper.insertSelective(entity) > 0) {
            return entity;
        }*/
        return null;
    }

    @Override
    @Transactional
    public SysInfo delete(Object id) {
        /*SysInfo one = getOne(id);
        if (one != null) {
            if (mapper.deleteByPrimaryKey((Integer) id) > 0) {
                return one;
            }
        }*/
        return null;
    }

    @Override
    @Transactional
    public boolean change(SysInfo entity) {
        return mapper.updateByPrimaryKeySelective(entity) > 0;
    }

    @Override
    public SysInfo getOne(Object id) {
        return mapper.selectByPrimaryKey((Integer) id);
    }

    public SysInfo findFirst() {
        List<SysInfo> sysInfos = mapper.selectByExample(null);
        if (!CollectionUtils.isEmpty(sysInfos)) {
            return sysInfos.get(0);
        }
        return null;
    }

    @Override
    public Page<SysInfo> getPage(int pageNo, int size, SysInfo entity) {
        return null;
    }
}
